package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.ht.medicaltreatment.bean.BorrowDetailBean
import com.ht.medicaltreatment.bean.BorrowQueryBean
import com.ht.medicaltreatment.bean.CreateBorrowBean
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class BorrowBuildViewModel : BaseViewModel() {

    fun create(expectedReturnDate:String,loanRemarks:String,orderName:String,projectId:String,takeLeave:String,onSuccess:(CreateBorrowBean)->Unit){
        viewModelScope.launch {
            val bean = mApi.createOrder(expectedReturnDate,loanRemarks,orderName,projectId,takeLeave)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    fun query(currentPage:Int,loanOrderId :String,name:String,projectId :String,
              sampleCategoryDictId:String,sign:String,onSuccess:(BorrowQueryBean)->Unit){
        viewModelScope.launch {
            val bean = mApi.queryBorrowPage(
                currentPage = currentPage,
                loanOrderId = loanOrderId,
                name = name,
                projectId = projectId,
                sampleCategoryDictId = sampleCategoryDictId,
                sign = sign,
                pageSize = "20"
            )
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    fun addSample(listLoanRfidSampleId:String,loanOrderId:String,rfidList:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.addSample(listLoanRfidSampleId,loanOrderId,rfidList)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    fun addSampleBox(listLoanRfidSampleBoxId:String,loanOrderId:String,projectId:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.addSampleBox(listLoanRfidSampleBoxId,loanOrderId,projectId)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    fun detailBorrow(orderId : Int,onSuccess:(BorrowDetailBean)->Unit){
        viewModelScope.launch {
            val bean = mApi.detailBorrow(orderId)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    fun deleteSmaple(loanSampleIdList:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.deleteSampleOrder(loanSampleIdList)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

    //确认借出
    fun jiechu(orderid:Int,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean = mApi.jiechu(orderid)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> dismissProgress(bean.errorMessage)
            }
        }
    }

}
